#include "SchoolLayer.h"
#include "Game.h"

SchoolLayer::SchoolLayer()
{

}

SchoolLayer::~SchoolLayer()
{

}

bool SchoolLayer::init()
{
    if (!Layer::init()) {
        return false;
    }

    EventDispatcher* eventDispatcher = Director::getInstance()->getEventDispatcher();
    auto listen = EventListenerTouchAllAtOnce::create();
    listen->onTouchesBegan = CC_CALLBACK_2(SchoolLayer::onTouchesBegan, this);
    eventDispatcher->addEventListenerWithSceneGraphPriority(listen, this);

    _visibleSize = Director::getInstance()->getVisibleSize();
    _origin = Director::getInstance()->getVisibleOrigin();
    initGameObject(_visibleSize, _origin);
    return initUIControl(_visibleSize, _origin);
}

void SchoolLayer::onEnter()
{
    Layer::onEnter();
    auto eventListener = EventListenerPhysicsContact::create();
    eventListener->onContactBegin = [this](PhysicsContact& contact){
        auto spriteA = (Sprite*)contact.getShapeA()->getBody()->getNode();
        auto spriteB = (Sprite*)contact.getShapeB()->getBody()->getNode();
        CCLOG("oh my god!");
        return true;
    };

    auto dispatchar = Director::getInstance()->getEventDispatcher();
    dispatchar->addEventListenerWithSceneGraphPriority(eventListener, this);
}

void SchoolLayer::onExit()
{
    Layer::onExit();
    Director::getInstance()->getEventDispatcher()->removeAllEventListeners();
}

bool SchoolLayer::initUIControl(const Size& visibleSize, const Vec2& origin)
{
    auto labelMessage = LabelTTF::create(Game::queryString("school_name"), "Arial", 24);
    labelMessage->setPosition(Vec2(100, 200));
    auto actionMoveByLabelSchool = MoveBy::create(5, Vec2(300, -100));
    auto actionMoveByLabelSchoolR = actionMoveByLabelSchool->reverse();
    auto funtionLabelSchool = CallFuncN::create([](Node* node){
        node->removeFromParent();
    });

    auto actionEq = Sequence::create(actionMoveByLabelSchool, actionMoveByLabelSchoolR, funtionLabelSchool, NULL);
    labelMessage->runAction(actionEq);
    this->addChild(labelMessage);
    return true;
}

bool SchoolLayer::initGameObject(const Size& visibleSize, const Vec2& origin)
{
    Sprite* gameA = Sprite::create("btn_disable.png", Rect(0, 0, 90, 40));
    auto gamePhysicsA = PhysicsBody::createBox(gameA->getContentSize());
    auto actionMoveByGameA = MoveBy::create(5, Vec2(100, 100));
    gamePhysicsA->setContactTestBitmask(0xFFFFFFFF);
    gameA->setPosition(Vec2(0, 0));
    gameA->setTag(1);
    gameA->setPhysicsBody(gamePhysicsA);
    gameA->runAction(actionMoveByGameA);

    Sprite* gameB = Sprite::create("btn_normal.png", Rect(0, 0, 90, 40));
    auto gamePhysicsB = PhysicsBody::createBox(gameB->getContentSize());
    gamePhysicsB->setContactTestBitmask(0xFFFFFFFF);
    gameB->setPosition(Vec2(100, 100));
    gameB->setTag(2);
    gameB->setPhysicsBody(gamePhysicsB);
    gameB->setName("gameB");

    this->addChild(gameA);
    this->addChild(gameB);
    return true;
}

void SchoolLayer::onTouchesBegan(const std::vector<Touch*>& touches, Event *event)
{
    auto gameB = this->getChildByName("gameB");
    if (gameB) {
        auto actionMoveBy = MoveTo::create(1, Vec2(touches[0]->getLocation().x, touches[0]->getLocation().y));
        gameB->runAction(actionMoveBy);
    }
}
